package WarTug;

import java.util.ArrayList;

public class Garden {

	public static final int GARDEN_WIDTH = 6;
	public static final int GARDEN_HEIGHT = 6;
	public final int GARDEN_SQUARE_WIDTH = 32;
	public int GARDEN_SCREEN_X;
	public final int GARDEN_SCREEN_Y = 408;
	public int playerNumber;
	// i,j pair of coords of selected Space.  i is the row, j is the column
	private int[] selectedSpace = new int[2];
	private GardenSpaceType[][] spaces = new GardenSpaceType[GARDEN_HEIGHT][GARDEN_WIDTH];
	
	
	public Garden () {} //Empty constructor is necessary to be able to pass this across the network
	
	public Garden(int screenX, int pNum){
		GARDEN_SCREEN_X = screenX;
		playerNumber = pNum;
		if (playerNumber == 0){
			selectedSpace[0] = 2;
			selectedSpace[1] = 5;
		} else {
			selectedSpace[0] = 2;
			selectedSpace[1] = 0;
		}
		for (int i = 0; i < GARDEN_WIDTH; i++){
			for (int j = 0; j < GARDEN_HEIGHT; j++){
				spaces[j][i] = GardenSpaceType.locked;
			}
		}
		
		if (playerNumber == 0){
			spaces[2][5] = GardenSpaceType.lion;
			spaces[3][5] = GardenSpaceType.lion;
			spaces[2][4] = GardenSpaceType.lion;
			spaces[3][4] = GardenSpaceType.lion;
		} else {
			spaces[2][0] = GardenSpaceType.lion;
			spaces[3][0] = GardenSpaceType.lion;
			spaces[2][1] = GardenSpaceType.lion;
			spaces[3][1] = GardenSpaceType.lion;
		}
	}
	
	public void setSpace(int i, int j, GardenSpaceType type){
		spaces[i][j] = type;
	}
	
	public void setSpace(GardenSpaceType type){
		spaces[selectedSpace[0]][selectedSpace[1]] = type;
	}
	
	public GardenSpaceType[][] getSpaces(){
		return spaces;
	}
	
	public int[] getSelectedSpace(){
		return selectedSpace;
	}
	
	public GardenSpaceType getSelectedSpaceType(){
		return spaces[selectedSpace[0]][selectedSpace[1]];
	}
	
	// The parameters are relative to the garden itself
	public void handleClick(int x, int y, GardenButtons gB){
		int i = x/GARDEN_SQUARE_WIDTH;
		int j = y/GARDEN_SQUARE_WIDTH;
		if (i >= 0 && i < GARDEN_WIDTH && j >= 0 && j < GARDEN_HEIGHT){
			if (spaces[j][i] != GardenSpaceType.locked){
				selectedSpace[0] = j;
				selectedSpace[1] = i;
				gB.refreshImages();
			} else if (adjacentToUnlocked(j, i)) {
				// Maybe do other stuff too... I'm not bad at coding
				selectedSpace[0] = j;
				selectedSpace[1] = i;
				gB.refreshImages();
			}
		}
	}
	
	public boolean adjacentToUnlocked(int j, int i){
		if (j-1 >= 0 && spaces[j-1][i] != GardenSpaceType.locked){
			return true;
		} else if (i+1 < GARDEN_WIDTH && spaces[j][i+1] != GardenSpaceType.locked){
			return true;
		} else if (j+1 < GARDEN_HEIGHT && spaces[j+1][i] != GardenSpaceType.locked){
			return true;
		} else if (i-1 >= 0 && spaces[j][i-1] != GardenSpaceType.locked){
			return true;
		} else 
			return false;
	}
	
	public ArrayList<SimpleUnit> spawnUnits() {
		ArrayList<SimpleUnit> retList = new ArrayList<SimpleUnit>();
		double startX = GARDEN_SCREEN_X + 16;
		double startY = 60 + 16;
		double spacing = GARDEN_SQUARE_WIDTH;
		int dirX;
		if (playerNumber == 0) {
			dirX = -1;
		} else {
			dirX = 1;
		}
		//Spawn units off-screen
		startX += spaces.length*spacing*dirX;
		
		for (int i = 0; i< spaces.length; i++) {
			for (int j = 0; j < spaces[i].length; j++) {
				UnitType t = gardenSpaceTypeToUnitType(spaces[i][j]);
				if (t != null) {
					switch (t) {
					case lion:
						retList.add(new UnitLion(startX+spacing*j, startY+spacing*i, dirX*-1, playerNumber));
						break;
					case gazelle:
						retList.add(new UnitGazelle(startX+spacing*j, startY+spacing*i, dirX*-1, playerNumber));
						break;
					case elephant:
						retList.add(new UnitElephant(startX+spacing*j, startY+spacing*i, dirX*-1, playerNumber));
						break;
					case ostrich:
						retList.add(new UnitOstrich(startX+spacing*j, startY+spacing*i, dirX*-1, playerNumber));
						break;
					}
				}
			}
		}
		
		return retList;
	}
	
	private UnitType gardenSpaceTypeToUnitType(GardenSpaceType t) {
		switch (t) {
		case lion:
			return UnitType.lion;
		case gazelle:
			return UnitType.gazelle;
		case elephant:
			return UnitType.elephant;
		case ostrich:
			return UnitType.ostrich;
		default:
			return null;
		}
		
	}
	
	
	
	
}
